package com.base.system.managedBean;

import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.html.impl.UIIFrame;
import org.operamasks.faces.component.layout.impl.UIWindow;
import org.operamasks.faces.component.widget.UIButton;
import org.operamasks.faces.component.widget.UISeparator;
import org.operamasks.faces.component.widget.UIToolBar;
import org.operamasks.faces.event.EventBroadcaster;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.Employee;
import com.base.organization.service.IPersonnelService;
import com.base.security.SecurityConst;
import com.base.security.dao.ISecurityUserDao;
import com.base.security.entity.SecurityUser;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_showSecurityUserBean", scope = ManagedBeanScope.REQUEST)
public class ShowSecurityUserBean {
	private final static Log logger = LogFactory
			.getLog(ShowSecurityUserBean.class);
	private ISecurityUserDao securityUserDAO = SpringBeanHolder
			.getBean("securityUserDao");
	private IPersonnelService personService = SpringBeanHolder
			.getBean("personService");
	@Inject
	FacesContext context;
	@Inject
	private EventBroadcaster event;
	@Accessible
	@SaveState
	private SecurityUser securityUser;
	@Bind(id = "detailDialog")
	protected UIWindow detailDialog;
	@Bind(id = "frame")
	private UIIFrame frame;
	@Bind
	private UISeparator separator;
	@Bind
	private UIToolBar toolbar_ope;
	@Bind(id = "createEmployee", attribute = "binding")
	private UIButton createEmployee_binding;

	@SaveState
	@Accessible
	private String loginTypeString;

	@Accessible
	@SaveState
	private boolean isUKUser = false;

	@Bind(id = "enabled", attribute = "value")
	private boolean enabled;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		// System.out.println("show 中的 postBack:" + isPostback);
		// 只有第一加载的时候才从session进行取值，后来提交后就不取值了
		if (!isPostback) {
			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();
			securityUser = (SecurityUser) session
					.get(SecurityConst.SELECT_ROW_DATA);
			Employee e = personService.getEmployeeOfUser(securityUser);
			String loginTypeIn = securityUser.getLoginType();
			if ("uk".equals(loginTypeIn)) {
				loginTypeString = "UK用户";
				isUKUser = true;
			} else {
				loginTypeString = "普通用户";
			}
			if (e != null) {
				separator.setRendered(false);
				createEmployee_binding.setRendered(false);
			}
		}

	}

	@Action(id = "remove")
	public void removeSecurityUser() {
		Map<String, Object> session = context.getExternalContext()
				.getSessionMap();
		securityUser = (SecurityUser) session
				.get(SecurityConst.SELECT_ROW_DATA);
		// System.out.println("删除：" + notice);
		securityUserDAO.delete(securityUser);
		session.remove(SecurityConst.SELECT_ROW_DATA);
		// 广播一个删除通知的事件，让noticeListBean中的删除通知Listener删除列表中的通知
		event.broadcast(this, "deleteSecurityUser", securityUser);
		String script = "closeCenterLayoutTab('" + SecurityConst.SHOW_TAB
				+ "')";
		Browser.execClientScript("window.top", script);
	}

	@Action(id = "modifyAccount")
	public void modifyAccount() {
		Map<String, Object> session = context.getExternalContext()
				.getSessionMap();
		securityUser = (SecurityUser) session
				.get(SecurityConst.SELECT_ROW_DATA);
		detailDialog.setTitle("修改用户");
		frame.load("/modules/system/editSecurityUserForWindow.faces");
		detailDialog.show();
	}

	@Action(id = "modifyDeparment")
	public void modifyDepartmentt() {
		Map<String, Object> session = context.getExternalContext()
				.getSessionMap();
		securityUser = (SecurityUser) session
				.get(SecurityConst.SELECT_ROW_DATA);
		detailDialog.setTitle("修改部门");
		frame.load("/modules/system/editDepAndPerson.faces");
		detailDialog.show();
	}

	@Action(id = "gridReload")
	public void gridReload() {
		Map<String, Object> session = context.getExternalContext()
				.getSessionMap();
		securityUser = (SecurityUser) session
				.get(SecurityConst.SELECT_ROW_DATA);
		detailDialog.close();
	}

	@Action(id = "createEmployee")
	public void createEmployee() {
		securityUser = (SecurityUser) context.getExternalContext()
				.getSessionMap().get(SecurityConst.SELECT_ROW_DATA);
		if (securityUser != null) {
			Employee e = new Employee();
			e.setUser(securityUser);
			personService.savePersonnel(e);
			separator.setRendered(false);
			createEmployee_binding.setRendered(false);
			toolbar_ope.repaint();
		}

	}
}
